package com.tshopping.controller;

import com.tshopping.bean.User;
import com.tshopping.service.UserService;
import com.tshopping.util.JWTUtil;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @name: LoginController
 * @description: 登录页面控制器
 * @author: Jackeys
 * @date: 2020-12-13 13:19
 */
@RestController
@RequestMapping("/LoginController")
@CrossOrigin
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    /**
     * @description 判断用户是否登录成功
     * @Param: email
     * @Param: password
     * @return User
     * @author Jackeys
     * @date 2020-12-15 15:41
     */
    public ResponseData login(String email, String password) {
        ResponseData responseData = ResponseData.ok();
        User user = userService.login(email, password);
        if (user != null) {
            //生成token
            String token = JWTUtil.generateToken("1", "Jersey-Security-Basic", user.getEmail());
            //向浏览器返回token，客户端受到此token后存入cookie中，或者h5的本地存储中
            responseData.putDataValue("token", token);
            //以及用户
            responseData.putDataValue("user", user);
        } else {
            //用户或者密码错误
            responseData = ResponseData.customerError();
        }
        return responseData;
    }
}
